package com.cf.api.config;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.md.common.utils.ApplicationContextProvider;
import com.md.common.utils.PasswordHelper;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.List;

import static com.alibaba.fastjson.serializer.SerializerFeature.WriteMapNullValue;

/**
 * 功能描述：加载bean 配置
 *
 * @author lujianping@oak168.com
 * @version 1.0
 * @date 2018-06-14 15:03
 * @since JDK 1.8
 */
@Configuration
public class BeanConfig {



    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastJsonConfig.setSerializerFeatures(WriteMapNullValue);
        fastConverter.setFastJsonConfig(fastJsonConfig);
//        HttpMessageConverter<?> converter = fastConverter;
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        supportedMediaTypes.add(MediaType.APPLICATION_PDF);
        supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XML);
        supportedMediaTypes.add(MediaType.IMAGE_GIF);
        supportedMediaTypes.add(MediaType.IMAGE_JPEG);
        supportedMediaTypes.add(MediaType.IMAGE_PNG);
        supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
        supportedMediaTypes.add(MediaType.TEXT_XML);
        fastConverter.setSupportedMediaTypes(supportedMediaTypes);

        return fastConverter;
//        ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
//        return new HttpMessageConverters(byteArrayHttpMessageConverter, converter);
    }


    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(10);
        threadPoolTaskExecutor.setMaxPoolSize(50);
        threadPoolTaskExecutor.setQueueCapacity(1000);

        threadPoolTaskExecutor.setKeepAliveSeconds(300);
        return threadPoolTaskExecutor;
    }

    @Bean
    public PasswordHelper passwordHelper(){
        PasswordHelper passwordHelper = new PasswordHelper();
        return  passwordHelper;
    }

    @Bean
    public ApplicationContextProvider applicationContextProvider() {
        return new ApplicationContextProvider();
    }

//    @Bean
//    public SolrServerProperties solrServerProperties() {
//        SolrServerProperties solrServerProperties = new SolrServerProperties();
//        solrServerProperties.setZkHosts(zkHosts);
//        return solrServerProperties;
//    }

    /**
     * redisson集群模式
     * @return
     */
//    @Bean
//    public RedissonClient redissonCluster() {
//        String[] redisClusterAddress = redssionProperties.getAddress().split(",");
//        Config config = new Config();
//        config.useClusterServers() //这是用的集群server
//                .setTimeout(redssionProperties.getTimeout())
//                .setScanInterval(2000) //设置集群状态扫描时间
//                .setMasterConnectionPoolSize(redssionProperties.getMasterConnectionPoolSize()) //设置连接数
//                .setSlaveConnectionPoolSize(redssionProperties.getSlaveConnectionPoolSize())
//                .addNodeAddress(redisClusterAddress).setPassword(redssionProperties.getPassword());
//        return Redisson.create(config);
//    }

    /**
     * 装配locker类
     * @return
     */
//    @Bean
//    public DistributedLocker distributedLocker(RedissonClient redissonClient) {
//        DistributedLocker locker = new RedissonDistributedLocker();
//        locker.setRedissonClient(redissonClient);
//        return locker;
//    }

}
